<?

//Data loading
$title='LEMBAR INFORMASI PEKERJAAN (LIB)';
if (GetParam("office","")) $perush = GetCompany (GetParam("office",""));
else $perush = 'PT. MASAJI PRAYASA CARGO';

$page   = 'PAGE';
$jo_id = GetParam("jo_id","");
$TableName = "tbljoborder";


$DEFINE_FONT=0;
$pdf=new PDFReport('P',"mm","Letter");
$pdf->AddPage();
//$Result = GridList ("", "", "");
//if ($Result) $pdf->GridTable($Result);
$pdf->Output();

function GridList ($Space, $Index, $ParentID) {
		    $DBConnection = new mydb(DB_USER, DB_PASSWORD, DB_NAME, DB_HOST);

			if ($Month<10) $Month = "0".$Month;
			if (($Month-1)<10) $LastMonth = "0".($Month-1);

			if ($Month<2) $LastMonth = "awal as lastmonth";
			else {$LastMonth = "aku".$LastMonth." as lastmonth";}
			
 			if (GetParam("fromDate","")) {
			    list($day,$month,$year) = explode("/",GetParam("fromDate",""));
			    $fromDate = date('Y-m-d',mktime(0,0,0,$month,$day,$year));
	 			$WHERE .= " AND tglbkt>='$fromDate'";
 		    }

 			if (GetParam("toDate","")) {
			    list($day,$month,$year) = explode("/",GetParam("toDate",""));
			    $toDate   = date('Y-m-d',mktime(0,0,0,$month,$day,$year));	 			
	 			$WHERE .= " AND tglbkt<='$toDate'";
 		    }
 		    
 		    if (GetParam("fromCOA","")) 
 		    	$WHERE .= " AND ais_memorial.skada = '".GetParam("fromCOA","")."'";


		    if (GetParam("office",""))
		    	$WHERE .= " AND ais_memorial.ktrasl='".GetParam("office","")."'";
		    	
 			$SQL  = "
	 			SELECT ais_memorial.nobkt,ais_memorial.debnam,ais_memorial.tglbkt,ais_memorial.skada,ais_memorial.uraian,ais_memorial.dk,ais_memorial.rupiah,$LastMonth, DATE_FORMAT(tglbkt,'%d/%m/%y') as trdate 
	 			FROM ais_memorial LEFT JOIN tblcoa ON ais_memorial.skada=tblcoa.skada
	 			WHERE 1 
 			";

 			//. 
 			if (strlen($WHERE)) $SQL .= $WHERE;
 			$SQL .= " ORDER BY ais_memorial.SKADA,ais_memorial.tglbkt";
 			//print $SQL;die();

			$MyResult = $DBConnection->dbc->get_results($SQL,ARRAY_A);
			return $MyResult;
}

function GetSaldoAwal($Month,$year) {
		    $DBConnection = new mydb(DB_USER, DB_PASSWORD, DB_NAME, DB_HOST);
			if ($Month<10) $Month = "0".$Month;
			if (($Month-1)<10) $LastMonth = "0".($Month-1);

			if ($Month<2) $LastMonth = "awal as lastmonth";
			else {$LastMonth = "aku".$LastMonth." as lastmonth";}

			$SQL = "SELECT $LastMonth FROM tblcoa WHERE ledger='$year'";
			//print $SQL;die();
			return $DBConnection->dbc->get_var($SQL);
}

function ReadCOA($no) {
		    $DBConnection = new mydb(DB_USER, DB_PASSWORD, DB_NAME, DB_HOST);
 			$SQL = "SELECT namacc FROM tblcoa WHERE skada='".$no."'";
			$MyResult = $DBConnection->dbc->get_var($SQL);
			return $MyResult;
}

function BuildPeriod ($year, $month) {
  $arraymonth = array("","JANUARI","FEBRUARI","MARET","APRIL","MEI","JUNI","JULI","AGUSTUS","SEPTEMBER","OKTOBER","NOPEMBER","DESEMBER");
  return $arraymonth[$month].", $year";
}


class PDFReport extends PDF {

	function Header() {
  	  global $title,$perush,$page,$DEFINE_FONT,$ROOT_IMG_DIR, $jo_id,$DBConnection,$DBGeneral,$TableName;
            
  	  if ($DEFINE_FONT==0) {
  		 $this->AddFont('arialn','','arialn.php');
  		 $DEFINE_FONT=1;
  	  }
      
      //Fetch Data Job Order
      $SQLJO = "SELECT * FROM ".$TableName." as a
                  JOIN tbldebtur as b on a.debtur = b.CUCODE 
                  JOIN ".$DBGeneral.".tblstaff as c on a.staff_id = c.staff_id 
                  JOIN tblproject as d on a.project_id = d.project_id 
                  JOIN $DBGeneral.tblstaff as e on a.staff_id = e.staff_id
                  WHERE a.jo_id ='".$jo_id."' ORDER BY a.jo_id ASC";
      
      $ResultJO  = $DBConnection->dbc->get_results($SQLJO,ARRAY_A); 
      
      
      //Fetch Data Profile
      $SQLProf = "SELECT * FROM tblprofile 
                  WHERE prsh ='MPC' ";
      
      $ResultProf  = $DBConnection->dbc->get_results($SQLProf,ARRAY_A); 
      
      
      //Cargo Count
      $SQLCargo = "SELECT * FROM tbljocargo
                WHERE jo_id ='".$jo_id."' ORDER BY jo_id ASC";
      
      $ResultCargo  = $DBConnection->dbc->get_results($SQLCargo,ARRAY_A); 
      
      $general_cargo = 0;
	  $heavy_lift_cargo = 0;
	  $container_20 = 0;
	  $container_40 = 0;  
	  
	  $cargo_jumlah = 0; 
	  $cargo_berat = 0; 
	  $cargo_volume = 0; 
	  $cargo_lamp = 0; 
      
      if(!empty($ResultCargo))
      {
      
		  foreach($ResultCargo as $cargo_id => $cargo_val)
		  {  
			 if($cargo_val['cargo_type'] == '4')
			 {
				$general_cargo+= intval($cargo_val['cargo_jumlah']); 			
			 }
			 
			 if($cargo_val['cargo_type'] == '5')
			 {
				$heavy_lift_cargo+= intval($cargo_val['cargo_jumlah']); 
			 }
			 
			 if($cargo_val['cargo_type'] == '6')
			 {
				$container_40+= intval($cargo_val['cargo_jumlah']); 
			 }
			 
			 if($cargo_val['cargo_type'] == '7')
			 {
				$container_20+= intval($cargo_val['cargo_jumlah']); 
			 }
			 
			 
			 $cargo_jumlah+= intval($cargo_val['cargo_jumlah']); 
			 $cargo_berat+= intval($cargo_val['cargo_berat']); 
			 $cargo_volume+= intval($cargo_val['cargo_volume']); 
			 $cargo_lamp = $cargo_val['cargo_lamp']; 
			 
			 
		  }
	  
      }
      
      
      //Cargo Count
      
      
      
      //Get the JO Date
      list($year1,$month1,$day1) = explode("-",$ResultJO[0]['jo_date']);
      $jo_date  = "$day1/$month1/$year1";
      
      //Get the Project Start Date
      list($year2,$month2,$day2) = explode("-",$ResultJO[0]['start_date']);
      $proj_start  = "$day2/$month2/$year2";
      
      //Get the Project End Date
      list($year3,$month3,$day3) = explode("-",$ResultJO[0]['end_date']);
      $proj_end  = "$day3/$month3/$year3";
      
      $shipment_arr = explode(".",$ResultJO[0]['jo_code']);
      $shipment = $shipment_arr[2];
      
    
             
      //End Fetch Data Job Order
           
	  $this->SetFont('arialn','',10);
	  
	  $this->Image($ROOT_IMG_DIR.'/flag_SI.jpg',12,11,34,20,'JPG');	  
	  $this->Cell(40,20,"",1,0,'C',0);
	  $this->SetFont('arialn','',18);
	  $this->Cell(110,20,"LEMBAR INFORMASI PEKERJAAN (LIP)",1,0,'C',0);
	  
	  $this->SetFont('arialn','',20);
	  $this->Cell(40,20,"MPC",1,0,'C',0);	  	  
	  $this->SetFont('arialn','',10);
	  
	  $this->Ln();	  
	  
	  $this->Cell(190,5,"JOB ORDER : ".$ResultJO[0]['jo_code'],1,0,'C',0);
	  
	  $this->Ln();	
	  $this->Ln();	
	  
	  $this->Cell(30,5,"Tanggal",1,0,'C',0);
	  $this->Cell(65,5,"30/8/2007",1,0,'C',0);
	  $this->Cell(30,5,"No. Dokumen",1,0,'C',0);
	  $this->Cell(65,5,$ResultJO[0]['doc_ref'],1,0,'C',0);
	        
      $this->Ln();
      
      $this->Cell(30,5,"Halaman",1,0,'C',0);
	  $this->Cell(65,5,"1",1,0,'C',0);
	  $this->Cell(30,5,"No. Revisi",1,0,'C',0);
	  $this->Cell(65,5,"0",1,0,'C',0);
      
      $this->Ln();
      $this->Ln();
      
      $this->Cell(30,5,"Revisi",1,0,'C',0);
      $this->Cell(25,5,"0",1,0,'C',0);
      $this->Cell(25,5,"1",1,0,'C',0);
      $this->Cell(25,5,"2",1,0,'C',0);
      $this->Cell(25,5,"3",1,0,'C',0);
      $this->Cell(60,5,"Status Pekerjaan",1,0,'C',0);
      
      $this->Ln();
      
      $this->Cell(30,5,"Tanggal",1,0,'C',0);
      $this->Cell(25,5,"",1,0,'C',0);
      $this->Cell(25,5,"",1,0,'C',0);
      $this->Cell(25,5,"",1,0,'C',0);
      $this->Cell(25,5,"",1,0,'C',0);
      $this->Cell(30,5,"Terlaksana",1,0,'C',0);
      $this->Cell(30,5,"Batal",1,0,'C',0);
      
      $this->Ln();
      $this->Ln();
      $this->Ln();
      
      //Text Form
      $this->Cell(50,5,"1. Nama Proyek",0,0,'L',0);
      $this->Cell(5,5,":",0,0,'C',0);
      $this->Cell(135,5,$ResultJO[0]['project_name'],0,0,'L',0);
      $this->Ln();
      $this->Cell(50,5,"2. Lokasi",0,0,'L',0);
      $this->Cell(5,5,":",0,0,'C',0);
      $this->Cell(135,5,$ResultJO[0]['project_location'],0,0,'L',0);
      $this->Ln();
      $this->Cell(50,5,"3. Schedule",0,0,'L',0);
      $this->Cell(5,5,":",0,0,'C',0);
      $this->Cell(135,5,$proj_start."  s/d  ".$proj_end,0,0,'L',0);
      $this->Ln();
      $this->Cell(50,5,"4. Customer",0,0,'L',0);
      $this->Cell(5,5,":",0,0,'C',0);
      $this->Cell(135,5,$ResultJO[0]['CUNAME'],0,0,'L',0);
      $this->Ln();
      $this->Cell(50,5,"Alamat and No.Telp",0,0,'R',0);
      $this->Cell(5,5,":",0,0,'C',0);
      $this->Cell(135,5,trim($ResultJO[0]['CUADDR'])." (phone : ".trim($ResultJO[0]['CUPHONE']).") ",0,0,'L',0);
      $this->Ln();
      $this->Cell(50,5,"PIC and No.HP",0,0,'R',0);
      $this->Cell(5,5,":",0,0,'C',0);
      $this->Cell(135,5,trim($ResultJO[0]['CUEMAIL'])." - ".trim($ResultJO[0]['CUCONTACT']),0,0,'L',0);
      $this->Ln();
      $this->Cell(50,5,"5. Data Cargo",0,0,'L',0);
      $this->Cell(5,5,":",0,0,'C',0);
      $this->Cell(135,5,"B/L : ".$ResultJO[0]['bl_no']."   /   Inv : ".$ResultJO[0]['invoice_no'],0,0,'L',0);
      $this->Ln();
      $this->Cell(50,5,"Shipment ke",0,0,'R',0);
      $this->Cell(5,5,":",0,0,'C',0);
      $this->Cell(135,5,$shipment."      Nama kapal : ".$ResultJO[0]['vessel'],0,0,'L',0);
      $this->Ln();
      $this->Ln();
      
      $this->Cell(50,5,"General Cargo : ".$general_cargo,1,0,'L',0);
      $this->Cell(50,5,"Heavy Lift Cargo :".$heavy_lift_cargo,1,0,'L',0);
      $this->Cell(45,5,"Container 20 feet :".$container_20,1,0,'L',0);
      $this->Cell(45,5,"Container 40 feet :".$container_40,1,0,'L',0);
      $this->Ln();
      
      $this->Cell(50,5,"Jumlah : ".$cargo_jumlah." pkgs",1,0,'L',0);
      $this->Cell(50,5,"Berat :".$cargo_berat." kgs",1,0,'L',0);
      $this->Cell(45,5,"Volume :".$cargo_volume." M3",1,0,'L',0);
      $this->Cell(45,5,"Remarks :".$cargo_lamp,1,0,'L',0);
      $this->Ln();
      $this->Ln();
      
      $this->Cell(190,5,"6. Penanggung Jawab Pekerjaan :",0,0,'L',0);
      $this->Ln();
      $this->Cell(35,5,"Bag.Marketing",1,0,'L',0);
      $this->Cell(30,5,"Paraf",1,0,'L',0);
      $this->Cell(35,5,"Bag.Operasi",1,0,'L',0);
      $this->Cell(30,5,"Paraf",1,0,'L',0);
      $this->Cell(30,5,"Bag.Traffic",1,0,'L',0);
      $this->Cell(30,5,"Paraf",1,0,'L',0);
      $this->Ln();
      $this->Cell(35,5,$ResultJO[0]['nama'],1,0,'L',0);
      $this->Cell(30,5,"",1,0,'L',0);
      $this->Cell(35,5,$ResultProf[0]['bag_operasi'],1,0,'L',0);
      $this->Cell(30,5,"",1,0,'L',0);
      $this->Cell(30,5,$ResultProf[0]['bag_traffic'],1,0,'L',0);
      $this->Cell(30,5,"",1,0,'L',0);
      $this->Ln();
      $this->Ln();
      
      $this->Cell(190,5,"7. Lingkup Pekerjaan :",0,0,'L',0);
      $this->Ln();
      
      //End Text Form
      
      
      //GRID
      
      
      $SQLSrvJO = "SELECT services_id FROM tbljoactivity
                   WHERE jo_id = '".$jo_id."'";
      
      $ResultSrvJO  = $DBConnection->dbc->get_results($SQLSrvJO,ARRAY_A); 
      
      if(!empty($ResultSrvJO))
      {
		  foreach($ResultSrvJO as $serv_id => $serv_val)
		  {
			 $SrvId[] =  $serv_val['services_id'];
		  }
      }
      
      
      $SQLSrv = "SELECT * FROM tblservices";      
      $ResultSrv  = $DBConnection->dbc->get_results($SQLSrv,ARRAY_A); 
      $it=0;
      
      if(!empty($ResultSrv))
      {
		  
		  foreach($ResultSrv as $serv_id => $serv_val)
		  {
			 if($it >= 2)
			 {
				$this->Ln();
				$it = 0; 
			 }
			 
			 if (in_array($serv_val['services_id'],$SrvId))
			 {
				$this->Cell(25,5,"V",1,0,'C',0);
				$this->Cell(70,5,$serv_val["services_name"],1,0,'L',0);      
			 }
			 
			 else 
			 {
				$this->Cell(25,5,"",1,0,'L',0);
				$this->Cell(70,5,$serv_val["services_name"],1,0,'L',0);       
			 }
			 
			 $it++;
		  }
	  
      }
	  
	  
	  //END GRID
      
         
     
	}

	function GridTable($MyResult) {
	    //Column widths
	    $w=array(25,20,45,20,40,25,25);

	    //Data


	    foreach($MyResult as $row)
	    {
		    if ($row["dk"]=='D') $row["rupiah1"] = $row["rupiah"];
		    else $row["rupiah2"] = $row["rupiah"];
		    $totald = $totald + $row["rupiah1"];
		    $totalc = $totalc + $row["rupiah2"];
		    
	        if (($skada<>$row["skada"]) && (strlen($skada)>0)) {
		        $this->Cell($w[0]+$w[1]+$w[2]+$w[3]+$w[4],5,"SUB TOTAL:",1,0,'R',0);
		        $this->Cell($w[5],5,number_format($subtotald+$SaldoDebet,2),1,0,'R',0);
				$this->Cell($w[6],5,number_format($subtotalc+$SaldoKredit,2),1,0,'R',0);
		        $this->Ln();
	        	$subtotald = 0;
	        	$subtotalc = 0;
	        	$this->AddPage();
        	}

		    //*********** SALDO AWAL *************
		    if (($skada<>$row["skada"])) {
			    $Saldo = $row["lastmonth"];
			    if ($Saldo>0) $SaldoDebet = $Saldo;
			    else $SaldoKredit = $Saldo;
	
			    $this->Cell($w[0]+$w[1]+$w[2]+$w[3]+$w[4],5,"SALDO AWAL (".$row["skada"]."):",'BTLR',0,'R');
			    $this->Cell($w[5],5,number_format($SaldoDebet,2),'LR',0,'R');
				$this->Cell($w[6],5,number_format($SaldoKredit,2),'LR',0,'R');
			    $this->Ln();
	    	}
			//*********** SALDO AWAL *************
        	
	        $subtotald = $subtotald + $row["rupiah1"];
	        $subtotalc = $subtotalc + $row["rupiah2"];

	        $this->Cell($w[0],5,$row["nobkt"],'LR');
	        $this->Cell($w[1],5,$row["trdate"],'LR',0,'C');
	        $this->Cell($w[2],5,$row["debnam"],'LR',0,'L');
	        $this->Cell($w[3],5,$row["skada"],'LR');
	        $this->Cell($w[4],5,$row["uraian"],'LR');
	        $this->Cell($w[5],5,number_format($row["rupiah1"],2),'LR',0,'R');
			$this->Cell($w[6],5,number_format($row["rupiah2"],2),'LR',0,'R');
	        $this->Ln();
	        $skada = $row["skada"];
	    }
		$this->Cell($w[0]+$w[1]+$w[2]+$w[3]+$w[4],5,"SUB TOTAL:",1,0,'R',0);
		$this->Cell($w[5],5,number_format($subtotald+$SaldoDebet,2),1,0,'R',0);
		$this->Cell($w[6],5,number_format($subtotalc+$SaldoKredit,2),1,0,'R',0);
		$this->Ln();
/*
	    $this->Cell(130,5,"TOTAL:",1,0,'R',0);
	    $this->Cell(25,5,number_format($totald,2),1,0,'R',0);
		$this->Cell(25,5,number_format($totalc,2),1,0,'R',0);
	    $this->Ln();
*/
	    //Closure line
	}

	function Footer() {
	    //Closure line
	    //$this->Cell(180,0,'','T');
	}

}


?>
